package de.acando.gui;



import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.springframework.data.mongodb.core.mapping.Document;

@Document (collection = "user")
public class Registrierung extends JFrame implements ActionListener {

	private JPanel panelName = new JPanel();
	private JPanel panelDaten = new JPanel();
	private JPanel panelCheckbox = new JPanel();
	private JPanel panelButton = new JPanel();
	
	private JLabel labelVorname = new JLabel("Vorname:");
	private JLabel labelNachname = new JLabel("Nachname:");
	private JLabel labelEmail = new JLabel("E-Mail:");
	private JLabel labelPasswort = new JLabel("Passwort:");
	private JLabel labelPasswortConfirm = new JLabel("Passwort bestätigen:");
	
	private JTextField textfieldVorname = new JTextField(20);
	private JTextField textfieldNachname = new JTextField(20);
	private JTextField textfieldEmail = new JTextField(18);
	private JPasswordField passwortField = new JPasswordField(20);
	private JPasswordField passwortFieldConfirm = new JPasswordField(20);
	
	private JCheckBox checkboxM = new JCheckBox("Maennlich");
	private JCheckBox checkboxW = new JCheckBox("Weiblich");
	
	private JButton buttonRegistrieren = new JButton("Registrieren");
	private JButton buttonAbbrechen = new JButton("Abbrechen");
	
	private String stringVorname = "";
	private String stringNachname = "";
	private String stringEmail = "";
	private String stringPasswort;
	
	public Registrierung(){
		bestandteileAnordnen();
		ereignissensorenAnmeldung();
		erstelleFenster();
	}
	
	private void bestandteileAnordnen(){
		GroupLayout RegistrierenLayout = new GroupLayout(getContentPane());
		getContentPane().setLayout(RegistrierenLayout);
		
		RegistrierenLayout.setAutoCreateGaps(true);
		RegistrierenLayout.setAutoCreateContainerGaps(true);
		
		RegistrierenLayout.setVerticalGroup(RegistrierenLayout.createSequentialGroup().
				addComponent(panelName).
				addComponent(panelDaten).
				addComponent(panelCheckbox).
				addComponent(panelButton));
		
		RegistrierenLayout.setHorizontalGroup(RegistrierenLayout.createParallelGroup().
				addComponent(panelName).
				addComponent(panelDaten).
				addComponent(panelCheckbox).
				addComponent(panelButton));
		
		panelDatenAnordnen();
		panelCheckboxAnordnen();
		panelButtonAnordnen();
	}
	
	private void panelDatenAnordnen(){
		GroupLayout panelDatenLayout = new GroupLayout(panelDaten);
		panelDaten.setLayout(panelDatenLayout);
		
		panelDatenLayout.setAutoCreateGaps(true);
		panelDatenLayout.setAutoCreateContainerGaps(true);
		
		panelDatenLayout.setVerticalGroup(panelDatenLayout.createSequentialGroup().
				addGroup(panelDatenLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).
						addComponent(labelVorname).
						addComponent(textfieldVorname)).
				addGroup(panelDatenLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).
						addComponent(labelNachname).
						addComponent(textfieldNachname)).
				addGroup(panelDatenLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).
						addComponent(labelEmail).
						addComponent(textfieldEmail)).
				addGroup(panelDatenLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).
						addComponent(labelPasswort).
						addComponent(passwortField)).
				addGroup(panelDatenLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).
						addComponent(labelPasswortConfirm).
						addComponent(passwortFieldConfirm)));
		
		panelDatenLayout.setHorizontalGroup(panelDatenLayout.createParallelGroup().addGroup(panelDatenLayout.createSequentialGroup().
				addGroup(panelDatenLayout.createParallelGroup(GroupLayout.Alignment.LEADING, true).
						addComponent(labelVorname, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(labelNachname, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(labelEmail, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(labelPasswort, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(labelPasswortConfirm, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).
				addGroup(panelDatenLayout.createParallelGroup(GroupLayout.Alignment.LEADING, true).
						addComponent(textfieldVorname, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(textfieldNachname, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(textfieldEmail, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(passwortField, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(passwortFieldConfirm, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
	}
	
	private void panelCheckboxAnordnen(){
		panelCheckbox.setLayout(new FlowLayout());
		panelCheckbox.add(checkboxW);
		panelCheckbox.add(checkboxM);
	}
	
	private void panelButtonAnordnen(){
		panelButton.setLayout(new FlowLayout());
		panelButton.add(buttonRegistrieren);
		panelButton.add(buttonAbbrechen);
	}
	
	private void erstelleFenster(){
		setTitle("Registrierung");
		setLocation(430, 250);
		pack();
		setVisible(true);
		setResizable(false);
	}
	
	private void ereignissensorenAnmeldung(){
		buttonRegistrieren.addActionListener(this);
		buttonAbbrechen.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		
		if(obj == buttonRegistrieren){
			getWerte();
			System.out.println("Vorname: "+stringVorname);
			new MongoDB(stringVorname, stringNachname, stringEmail, stringPasswort);
			//dispose();
			//new Home();
		}
		
		if(obj == buttonAbbrechen){
			dispose();
		}
		
	}
	
	private void getWerte(){
		this.stringVorname = textfieldVorname.getText();
		this.stringNachname = textfieldNachname.getText();
		this.stringEmail = textfieldEmail.getText();
		this.stringPasswort = passwortField.getText();
	}
	
	public static void main(String[] args) {

		new Registrierung();
	}

}
